using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

using EntLibEC.Cmn;
using EntLibEC.Info.Basic;
using EntLibEC.Biz;
using EntLibEC.Biz.Basic;

namespace EntLibEC.View.Cmn
{
	/// <summary>
	/// Summary description for PayType.
	/// </summary>
	public class PayType : Control, INamingContainer
	{
		private DropDownList ddlPayType = new DropDownList();

		public void BindPayType(bool isAll)
		{
			SortedList sl = ASPManager.GetInstance().GetPayTypeList(false);
			BindDdl(sl, isAll);
		}

        public void BindPayType(bool isAll, bool isOnlineShow)
        {
            SortedList sl = ASPManager.GetInstance().GetPayTypeList(isOnlineShow);
            BindDdl(sl, isAll);
        }

		private void BindDdl(SortedList sl, bool isAll)
		{
			ddlPayType.Items.Clear();
			if ( sl != null && sl.Count != 0 )
			{
				foreach(PayTypeInfo item in sl.Values)
				{
					ddlPayType.Items.Add(
						new ListItem(item.PayTypeName, item.SysNo.ToString()) );
				}
			}
			if ( isAll )
				ddlPayType.Items.Insert(0, new ListItem( AppConst.AllSelectString, AppConst.IntNull.ToString() ));
		}
		public void BindPayTypeByShipType(int shipTypeSysNo)
		{
			SortedList sl = ASPManager.GetInstance().GetPayTypeListByShipType(shipTypeSysNo);
			BindDdl(sl, true);
		}
		public void BindOnlyNetPay(bool isAll)
		{
			SortedList sl = ASPManager.GetInstance().GetPayTypeListIsPayAdvanced(true);
			BindDdl(sl, isAll);
		}
		public void BindAdvancedPayWithoutNetPay(bool isAll)
		{
			SortedList sl = ASPManager.GetInstance().GetPayTypeListIsPayAdvanced(false);
			BindDdl(sl, isAll);
		}
		public int PayTypeSysNo
		{
			get
			{
				try
				{
					return Convert.ToInt32(ddlPayType.SelectedItem.Value);
				}
				catch
				{
					return AppConst.IntNull;
				}
			}
			set
			{
				if(ddlPayType.Items.FindByValue(value.ToString())!=null)
					ddlPayType.SelectedIndex = ddlPayType.Items.IndexOf(ddlPayType.Items.FindByValue(value.ToString()));
				else
					ddlPayType.SelectedIndex = ddlPayType.Items.IndexOf(ddlPayType.Items.FindByValue(AppConst.IntNull.ToString()));
			}
		}

		private bool _autoPostBack;
		public bool AutoPostBack
		{
			get
			{
				return _autoPostBack;
			}
			set
			{
				_autoPostBack = value;
			}

		}

		protected override void OnInit(EventArgs e)
		{
			if ( AutoPostBack)
			{
				ddlPayType.AutoPostBack = true;
				ddlPayType.SelectedIndexChanged += new System.EventHandler(ddl_SelectedIndexChanged);
			}

			Controls.Add(ddlPayType);

			base.OnInit(e);
		}

		public void ddl_SelectedIndexChanged(object sender, System.EventArgs e)
		{

			if (SelectedIndexChanged != null)
				SelectedIndexChanged(this, EventArgs.Empty);
		}

		public bool Enabled
		{
			set
			{
				ddlPayType.Enabled = value;
			}
		}

		public event EventHandler SelectedIndexChanged;
	}
}
